/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary
{
    public class Namespace
    {
        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Binding m_Binding;

        private string m_URI;
        private string m_LocalPrefix;
        private string m_MappedPrefix;

        private System.Xml.XmlElement m_XmlElement;

        public Namespace(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Binding i_Binding, string i_LocalPrefix, string i_URI)
        {
            this.NamespaceInitialize
                (
                i_LocalPrefix,
                i_URI
                );

            string ElementName = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.NAMESPACE_ELEMENT_NAME;
            if (!System.String.IsNullOrEmpty(i_Binding.NamespacePrefix))
                ElementName = i_Binding.NamespacePrefix + ":" + ElementName;

            m_XmlElement = i_Binding.XmlElement.OwnerDocument.CreateElement
                (
                ElementName,
                Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI
                );

            m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.LOCAL_PREFIX_ATTRIBUTE, m_LocalPrefix);
            m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.URI_ATTRIBUTE, m_URI);
        }

        public Namespace(System.Xml.XmlElement i_NamespaceXmlElement)
        {
            if (i_NamespaceXmlElement == null)
                throw new System.ArgumentException("i_NamespaceXmlElement must be non-null!");

            bool InvalidElement =
                (i_NamespaceXmlElement.LocalName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.NAMESPACE_ELEMENT_NAME) ||
                (i_NamespaceXmlElement.NamespaceURI != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI);

            if (InvalidElement)
                throw new System.ArgumentException("Invalid i_NamespaceXmlElement!");

            m_XmlElement = i_NamespaceXmlElement;

            string URI = m_XmlElement.GetAttribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.URI_ATTRIBUTE);
            string LocalPrefix = m_XmlElement.GetAttribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.LOCAL_PREFIX_ATTRIBUTE);

            this.NamespaceInitialize
                (
                LocalPrefix,
                URI
                );
        }

        internal void NamespaceInitialize(string i_LocalPrefix, string i_URI)
        {
            if (System.String.IsNullOrEmpty(i_URI))
                throw new System.ArgumentException("Must provide non-null values for id and uri!");

            if (System.String.IsNullOrEmpty(i_LocalPrefix))
                throw new System.ArgumentException("Must provide non-null values for id and uri!");

            m_URI = i_URI;
            m_LocalPrefix = i_LocalPrefix;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Binding Binding
        {
            get
            {
                return m_Binding;
            }

            internal set
            {
                if (value == null)
                    throw new System.ArgumentException("Value must be non-null!");

                m_Binding = value;
            }
        }

        public string URI
        {
            get
            {
                return m_URI;
            }
        }

        public string LocalPrefix
        {
            get
            {
                return m_LocalPrefix;
            }
        }

        public string MappedPrefix
        {
            get
            {
                return m_MappedPrefix;
            }
        }


        public System.Xml.XmlElement XmlElement
        {
            get
            {
                return m_XmlElement;
            }
        }

        internal void SetMappedPrefix(string i_MappedPrefix)
        {
            if (System.String.IsNullOrEmpty(i_MappedPrefix))
                throw new System.ArgumentException("Invalid value for i_MappedPrefix!");

            m_MappedPrefix = i_MappedPrefix;
        }
    }
}
